<?php
/**
 * 登录接口
 */
namespace Api\Controller;

use Think\Controller;

class LoginController extends Controller {

    public function index() {
        
    	$name = I("post.username");
    	if(empty($name)){
	    	exit(json_return('0','请输入用户名'));
    	}
    	
    	$pass = I("post.password");
    	if(empty($pass)){
	    	exit(json_return('0','请输入密码'));
    	}

		$user = D("Common/Users");
		$where['user_login']=$name;
		
		$result = $user->where($where)->find();
		if(empty($result) || $result['user_type']!=1 || $result['model'] != 3){
	    	exit(json_return('0','用户名不存在'));
		}

		if(!sp_compare_password($pass,$result['user_pass'])){
	    	exit(json_return('0','密码错误！'));
		}

		$role_user_model=M("RoleUser");
		$role_user_join = C('DB_PREFIX').'role as b on a.role_id =b.id';
		$groups=$role_user_model->alias("a")->join($role_user_join)->where(array("user_id"=>$result["id"],"status"=>1))->getField("role_id",true);
		
		if( $result["id"]!=1 && ( empty($groups) || empty($result['user_status']) ) ){
	    	exit(json_return('0','此用户无法登录，请联系管理员'));
		}

		// 记录登录信息
		$result['last_login_ip']=get_client_ip(0,true);
		$result['last_login_time']=date("Y-m-d H:i:s");
		$user->save($result);

		$data = array();
		$data['user_id'] = $result["id"];
		$data['sale_id'] = $result["organize_id"];
		$data['user_name'] = $result["user_login"];

    	exit(json_return('1','',$data));
    }

}

